package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.system.domain.Materials;
import com.ruoyi.system.domain.WarehouseComeDetails;
import com.ruoyi.system.domain.WarehouseOutDetails;
import com.ruoyi.system.mapper.MaterialsMapper;
import com.ruoyi.system.mapper.WarehouseComeDetailsMapper;
import com.ruoyi.system.service.IMaterialsService;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.WarehouseComeMapper;
import com.ruoyi.system.domain.WarehouseCome;
import com.ruoyi.system.service.IWarehouseComeService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 入库表Service业务层处理
 *
 * @author ruoyi
 * @date 2023-05-26
 */
@Service
public class WarehouseComeServiceImpl implements IWarehouseComeService {
    @Autowired
    private WarehouseComeMapper warehouseComeMapper;

    @Autowired
    private WarehouseComeDetailsMapper warehouseComeDetailsMapper;

    @Autowired
    private MaterialsMapper materialsMapper;

    /**
     * 查询入库表
     *
     * @param warehouseComeId 入库表主键
     * @return 入库表
     */
    @Override
    public WarehouseCome selectWarehouseComeByWarehouseComeId(String warehouseComeId) {
        WarehouseCome warehouseCome = warehouseComeMapper.selectWarehouseComeByWarehouseComeId(warehouseComeId);
        warehouseCome.setWarehouseComeDetailsList(warehouseComeDetailsMapper.selectWarehouseComeDetailsListByWarehouseComeId(warehouseComeId));
        return warehouseCome;
    }

    /**
     * 查询入库表列表
     *
     * @param warehouseCome 入库表
     * @return 入库表
     */
    @Override
    public List<WarehouseCome> selectWarehouseComeList(WarehouseCome warehouseCome) {
        return warehouseComeMapper.selectWarehouseComeList(warehouseCome);
    }

    /**
     * 新增入库表
     *
     * @param warehouseCome 入库表
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertWarehouseCome(WarehouseCome warehouseCome) {
        warehouseCome.setWarehouseComeId(IdUtils.simpleUUID());
        if (StringUtils.isNotEmpty(warehouseCome.getWarehouseComeDetailsList()) && warehouseCome.getWarehouseComeDetailsList().size() > 0) {
            warehouseCome.getWarehouseComeDetailsList().stream()
                    .map(item -> {
                        item.setWarehouseComeDetailsId(IdUtils.simpleUUID());
                        item.setWarehouseComeId(warehouseCome.getWarehouseComeId());
                        return item;
                    })
                    .collect(Collectors.toList());
            for (WarehouseComeDetails warehouseComeDetails : warehouseCome.getWarehouseComeDetailsList()) {
                Materials materials = new Materials();
                materials.setMaterialsId(IdUtils.simpleUUID());
                materials.setMaterialsName(warehouseComeDetails.getMaterialsName());
                materials.setMaterialsCategory(warehouseComeDetails.getMaterialsCategory());
                materials.setMaterialsCount(warehouseComeDetails.getMaterialsCount());
                materials.setMaterialsPrice(warehouseComeDetails.getMaterialsPrice());
                materials.setMaterialsUnit(warehouseComeDetails.getMaterialsUnit());
                materials.setMaterialsDescribe(warehouseComeDetails.getMaterialsDescribe());
                materials.setMaterialsPicture(warehouseComeDetails.getMaterialsPicture());
                materials.setMaterialsBatch(warehouseComeDetails.getMaterialsBatch());
                materials.setMaterialsBatchNumber(warehouseComeDetails.getMaterialsBatchNumber());
                materials.setStorageLocationId(warehouseComeDetails.getWarehouseComeMaterialsTargetStorageLocation());
                materials.setWarehouseId(warehouseComeDetails.getWarehouseComeMaterialsTargetWarehouse());
                materialsMapper.insertMaterials(materials);
            }
            warehouseComeDetailsMapper.insertBatchWarehouseComeDetails(warehouseCome.getWarehouseComeDetailsList());
        }
        return warehouseComeMapper.insertWarehouseCome(warehouseCome);
    }

    /**
     * 修改入库表
     * 1.先查出来当前入库操作的所有的入库记录，然后和修改后的数据进行对比，如果少了，进行删除操作和物品的修改
     * 2,如果数据存在进行更新
     * 3.如果
     *
     * @param warehouseCome 入库表
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateWarehouseCome(WarehouseCome warehouseCome) {
        //当前入库的库存记录
        List<WarehouseComeDetails> warehouseComeDetails = warehouseComeDetailsMapper.selectWarehouseComeDetailsListByWarehouseComeId(warehouseCome.getWarehouseComeId());
        //新增的库存记录-id为null
        List<WarehouseComeDetails> collectAdd = warehouseCome.getWarehouseComeDetailsList().stream().filter(n -> StringUtils.isEmpty(n.getWarehouseComeDetailsId())).collect(Collectors.toList());
        //删除的库存记录-对物品表的数据进行相减
        List<WarehouseComeDetails> collectDel = warehouseComeDetails.stream().filter(n ->
                warehouseCome.getWarehouseComeDetailsList().stream().filter(k -> StringUtils.isNotEmpty(k.getWarehouseComeDetailsId())).collect(Collectors.toList())
                        .stream().noneMatch(h -> h.getWarehouseComeDetailsId().equals(n.getWarehouseComeDetailsId()))
        ).collect(Collectors.toList());
        //更新的库存记录-对物品表的数量进行修改
        List<WarehouseComeDetails> collectUpdate = warehouseCome.getWarehouseComeDetailsList().stream().filter(k -> StringUtils.isNotEmpty(k.getWarehouseComeDetailsId())).collect(Collectors.toList());



        //对id为null,进行新增
        for (WarehouseComeDetails n : collectAdd) {
            n.setWarehouseComeDetailsId(IdUtils.simpleUUID());
            n.setWarehouseComeId(warehouseCome.getWarehouseComeId());
            Materials materials = new Materials();
            materials.setMaterialsId(IdUtils.simpleUUID());
            materials.setMaterialsName(n.getMaterialsName());
            materials.setMaterialsCategory(n.getMaterialsCategory());
            materials.setMaterialsCount(n.getMaterialsCount());
            materials.setMaterialsPrice(n.getMaterialsPrice());
            materials.setMaterialsUnit(n.getMaterialsUnit());
            materials.setMaterialsDescribe(n.getMaterialsDescribe());
            materials.setMaterialsPicture(n.getMaterialsPicture());
            materials.setMaterialsBatch(n.getMaterialsBatch());
            materials.setMaterialsBatchNumber(n.getMaterialsBatchNumber());
            materials.setStorageLocationId(n.getWarehouseComeMaterialsTargetStorageLocation());
            materials.setWarehouseId(n.getWarehouseComeMaterialsTargetWarehouse());
            materialsMapper.insertMaterials(materials);
        }

        if (StringUtils.isNotEmpty(collectAdd) && collectAdd.size() > 0) {
            warehouseComeDetailsMapper.insertBatchWarehouseComeDetails(collectAdd);
        }
        //对前端删除的数据进行物品表的数量减少-并删除入库记录
        for (WarehouseComeDetails n : collectDel) {
            Materials materials = new Materials();
            materials.setMaterialsId(IdUtils.simpleUUID());
            materials.setMaterialsName(n.getMaterialsName());
            materials.setMaterialsCategory(n.getMaterialsCategory());
            materials.setMaterialsCount(-n.getMaterialsCount());
            materials.setMaterialsPrice(n.getMaterialsPrice());
            materials.setMaterialsUnit(n.getMaterialsUnit());
            materials.setMaterialsDescribe(n.getMaterialsDescribe());
            materials.setMaterialsPicture(n.getMaterialsPicture());
            materials.setMaterialsBatch(n.getMaterialsBatch());
            materials.setMaterialsBatchNumber(n.getMaterialsBatchNumber());
            materials.setStorageLocationId(n.getWarehouseComeMaterialsTargetStorageLocation());
            materials.setWarehouseId(n.getWarehouseComeMaterialsTargetWarehouse());
            materialsMapper.insertMaterials(materials);
        }
        if (StringUtils.isNotEmpty(collectDel) && collectDel.size() > 0) {
            warehouseComeDetailsMapper.deleteWarehouseComeDetailsByWarehouseComeDetailsIds(collectDel.stream().map(WarehouseComeDetails::getWarehouseComeDetailsId).toArray(String[]::new));
        }

        //对需要更新的数据进行物品表的更新
        for (WarehouseComeDetails n : collectUpdate) {
            WarehouseComeDetails warehouseComeDetails1 = warehouseComeDetails.stream().filter(k -> StringUtils.isNotEmpty(k.getWarehouseComeDetailsId()) &&
                    k.getWarehouseComeDetailsId().equals(n.getWarehouseComeDetailsId()))
                    .findFirst().orElse(null);

            Materials materials = new Materials();
            materials.setMaterialsId(IdUtils.simpleUUID());
            materials.setMaterialsName(n.getMaterialsName());
            materials.setMaterialsCategory(n.getMaterialsCategory());
            if (warehouseComeDetails1 != null) {
                materials.setMaterialsCount(n.getMaterialsCount() - warehouseComeDetails1.getMaterialsCount());
            }
            materials.setMaterialsPrice(n.getMaterialsPrice());
            materials.setMaterialsUnit(n.getMaterialsUnit());
            materials.setMaterialsDescribe(n.getMaterialsDescribe());
            materials.setMaterialsPicture(n.getMaterialsPicture());
            materials.setMaterialsBatch(n.getMaterialsBatch());
            materials.setMaterialsBatchNumber(n.getMaterialsBatchNumber());
            materials.setStorageLocationId(n.getWarehouseComeMaterialsTargetStorageLocation());
            materials.setWarehouseId(n.getWarehouseComeMaterialsTargetWarehouse());
            materialsMapper.insertMaterials(materials);
            warehouseComeDetailsMapper.updateWarehouseComeDetails(n);
        }

        return warehouseComeMapper.updateWarehouseCome(warehouseCome);
    }


    /**
     * 批量删除入库表
     *
     * @param warehouseComeIds 需要删除的入库表主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteWarehouseComeByWarehouseComeIds(String[] warehouseComeIds) {
        for (String warehouseComeId : warehouseComeIds) {
            List<WarehouseComeDetails> warehouseComeDetails = warehouseComeDetailsMapper.selectWarehouseComeDetailsListByWarehouseComeId(warehouseComeId);
            for (WarehouseComeDetails n : warehouseComeDetails) {
                Materials materials = new Materials();
                materials.setMaterialsId(IdUtils.simpleUUID());
                materials.setMaterialsName(n.getMaterialsName());
                materials.setMaterialsCategory(n.getMaterialsCategory());
                materials.setMaterialsCount(-n.getMaterialsCount());
                materials.setMaterialsPrice(n.getMaterialsPrice());
                materials.setMaterialsUnit(n.getMaterialsUnit());
                materials.setMaterialsDescribe(n.getMaterialsDescribe());
                materials.setMaterialsPicture(n.getMaterialsPicture());
                materials.setMaterialsBatch(n.getMaterialsBatch());
                materials.setMaterialsBatchNumber(n.getMaterialsBatchNumber());
                materials.setStorageLocationId(n.getWarehouseComeMaterialsTargetStorageLocation());
                materials.setWarehouseId(n.getWarehouseComeMaterialsTargetWarehouse());
                materialsMapper.insertMaterials(materials);
            }
            warehouseComeDetailsMapper.deleteWarehouseComDetailsByWarehouseComeId(warehouseComeId);
        }
        return warehouseComeMapper.deleteWarehouseComeByWarehouseComeIds(warehouseComeIds);
    }

    /**
     * 删除入库表信息
     *
     * @param warehouseComeId 入库表主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int deleteWarehouseComeByWarehouseComeId(String warehouseComeId) {
        warehouseComeDetailsMapper.deleteWarehouseComDetailsByWarehouseComeId(warehouseComeId);
        return warehouseComeMapper.deleteWarehouseComeByWarehouseComeId(warehouseComeId);
    }
}
